package com.example.springboottest.example.thread.volatiletest;

import java.util.concurrent.*;

public class AAA {

    public static void main(String[] args) throws InterruptedException {
        ThreadPoolExecutor rejectedThreadPool = new ThreadPoolExecutor(
                1,
                1,
                0L,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100));

        try {
            for (int i = 0; i < 102; i++) {
                rejectedThreadPool.execute(() -> {
                    ThreadLocal<Integer> count = new ThreadLocal<>();
                    count.set(0);
                    while (true) {
                        if (count.get() == 2) {
                            int m = 1 / 0;
                        }
                        count.set(count.get() + 1);
                        System.out.println(Thread.currentThread().getName() + "号线程办理业务" + "\tcount=" + count.get());
                        try {
                            TimeUnit.SECONDS.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        while (true) {
//            System.out.println("核心线程数" + rejectedThreadPool.getCorePoolSize());
//            System.out.println("最大线程数" + rejectedThreadPool.getMaximumPoolSize());
            System.out.println("线程池数" + rejectedThreadPool.getPoolSize());
            System.out.println("队列任务数" + rejectedThreadPool.getQueue().size());
            TimeUnit.SECONDS.sleep(5);
//            if (rejectedThreadPool.getPoolSize() < 1) {
//            rejectedThreadPool.execute(() -> {
//                while (true) {
//                    System.out.println(Thread.currentThread().getName() + "号线程办理业务");
//                    try {
//                        TimeUnit.SECONDS.sleep(5);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            });
//            }
        }
    }

}